package com.expoente.view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.expoente.model.LineInformation;
import com.expoente.model.Place;
import com.expoente.model.dao.impl.DBAdapterImpl;
import com.expoente.utils.DroidBusUtils;

public class LocationLinesActivity
		extends Activity {

	private Spinner lineSpinner;

	private static Location lastLocation;

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.location_lines_activity);

		DBAdapterImpl dbAdapter = new DBAdapterImpl(this);
		dbAdapter.open();
		ArrayList<LineInformation> lines = dbAdapter.getAllLinesInformation();
		ArrayList<LineInformation> nextLines = new ArrayList<LineInformation>();
		do {
			lastLocation = DroidBusUtils.getLocation(this);
		} while (lastLocation.getAccuracy() > 300);
		Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());

		for (LineInformation lineInformation : lines) {
			for (Place place : lineInformation.getPlaces()) {
				try {
					if (0 == place.getLatitude() && 0 == place.getLongitude()) {
						List<Address> pontoInicial = geoCoder
								.getFromLocationName(place.getAddress(), 5);
						if (pontoInicial.size() > 0) {
							place
									.setLatitude(pontoInicial.get(0)
											.getLatitude());
							place.setLongitude(pontoInicial.get(0)
									.getLongitude());
							dbAdapter.updatePlaceLatLong(place.get_id(), place
									.getLatitude(), place.getLongitude());
						}
					}
					if (DroidBusUtils.getDistance(lastLocation.getLatitude(),
							lastLocation.getLongitude(), place.getLatitude(),
							place.getLongitude())) {
						nextLines.add(lineInformation);
						break;
					}
				} catch (IOException io) {
					Log.d("ERROR", "Location not found");
				}
			}
		}
		dbAdapter.close();

		lineSpinner = (Spinner) findViewById(R.id.lineSpinner);
		ArrayAdapter<LineInformation> adapter = new ArrayAdapter<LineInformation>(
				this, android.R.layout.simple_spinner_dropdown_item, nextLines);
		adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		lineSpinner.setAdapter(adapter);

		Button viewDetailsButton = (Button) findViewById(R.id.viewDetailsButton);
		viewDetailsButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				Intent intent = new Intent(view.getContext(),
						LineReviewActivity.class);
				LineInformation lineInformation = (LineInformation) lineSpinner
						.getSelectedItem();
				intent.putExtra("item", lineInformation);
				startActivity(intent);
			}
		});

	}
	
}
